#include "b_config.h"
#include "b_os.h"
#include "ciu32l051_std.h"
#include "ciu32l051_std_rcc.h"
#include "ciu32l051_std_rtc.h"
#include "hal/inc/b_hal_rtc.h"
/**
 * @brief  RTCÊ±ÖÓÉèÖÃ
 * @retval ÎÞ
 */
void rtc_clock_config(void)
{
    /* RTC APBÊ±ÖÓÊ¹ÄÜ */
    std_rcc_apb1_clk_enable(RCC_PERIPH_CLK_RTC);

    /* Ê¹ÄÜPMUÊ±ÖÓ£¬¿ªÆôVCORE_AONÐ´Ê¹ÄÜ */
    std_rcc_apb1_clk_enable(RCC_PERIPH_CLK_PMU);
    std_pmu_vaon_write_enable();

    /* Ê¹ÄÜLXTALÊ±ÖÓ */
    std_rcc_lxtal_drive_mode_config(RCC_LXTAL_DRIVE_MODE_NORMAL);
    std_rcc_lxtal_drive_config(RCC_LXTAL_DRIVE_LEVEL0);
    std_rcc_lxtal_enable(RCC_LXTAL_ON);
    while (!std_rcc_get_lxtal_ready())
    {
        //b_log("lxtal not ready\n");
        //超时100ms退出
        bHalDelayMs(10);
        break;
    };

    /* Ñ¡ÔñLXTAL×÷ÎªRTCÊ±ÖÓÔ´

       ×¢Òâ: Ñ¡ÔñRTCÊ±ÖÓÔ´ºó£¬ÒÔÏÂÇé¿ö¿ÉÖØÐÂÅäÖÃRTCÊ±ÖÓÔ´£º
             - VCORE_AONÓò¸´Î»
    */
    std_rcc_set_rtcclk_source(RCC_RTC_ASYNC_CLK_SRC_LXTAL);

    /* RTCÍâÉèÊ±ÖÓÊ¹ÄÜ */
    std_rcc_rtc_enable();
}
/**
 * @brief  RTCÈÕÆÚÊ±¼äÅäÖÃ
 * @retval ÎÞ
 */
void bsp_rtc_datetime_config(void)
{
    std_rtc_time_t rtc_time = {0};
    std_rtc_date_t rtc_date = {0};
    std_status_t   status   = STD_ERR;

    /* RTCÈÕÆÚ£¨BCDÂë£©³õÊ¼»¯ */
    rtc_date.weekday = 3;
    rtc_date.month   = 5;
    rtc_date.day     = 0x10;
    rtc_date.year    = 0x25;

    status = std_rtc_date_init(&rtc_date);
    while (status != STD_OK)
    {

    }

    rtc_time.hours   = 0x16;
    rtc_time.minutes = 0x20;
    rtc_time.seconds = 0x05; 

    status = std_rtc_time_init(&rtc_time);
    while (status != STD_OK)
    {

    }
}
int bMcuRtcInit(void)
{
    rtc_clock_config();
    //bsp_rtc_datetime_config();
    return 0;
}

int bMcuRtcSetTime(bRtcTime_t *time)
{
    std_rtc_time_t rtc_time = {0};
    std_rtc_date_t rtc_date = {0};
    std_status_t   status   = STD_ERR;

    rtc_time.hours   = std_rtc_convert_bin2bcd(time->hour);
    rtc_time.minutes = std_rtc_convert_bin2bcd(time->minute);
    rtc_time.seconds = std_rtc_convert_bin2bcd(time->second);

    rtc_date.year    = std_rtc_convert_bin2bcd(time->year);
    rtc_date.month   = std_rtc_convert_bin2bcd(time->month);
    rtc_date.day     = std_rtc_convert_bin2bcd(time->day);
    //0代表星期7 需要转换
    rtc_date.weekday = (time->weekday == 7) ? 0 : time->weekday;
   //打印所有参数
  // b_log("rtc_date.year: %x, rtc_date.month: %x, rtc_date.day: %x, rtc_date.weekday: %x\n", rtc_date.year, rtc_date.month, rtc_date.day, rtc_date.weekday);
  // b_log("rtc_time.hours: %x, rtc_time.minutes: %x, rtc_time.seconds: %x\n", rtc_time.hours, rtc_time.minutes, rtc_time.seconds);
    status = std_rtc_date_init(&rtc_date);
    // while (status != STD_OK)
    // {
    //     b_log("rtc_date_init failed\n");
    // }

    status = std_rtc_time_init(&rtc_time);
    // while (status != STD_OK)
    // {
    //     b_log("rtc_time_init failed\n");
    // }

    return 0;
}

int bMcuRtcGetTime(bRtcTime_t *time)
{
#define TIMES_TO_GET_TR (0x03)
    uint32_t i, read_ok = 0;
    uint32_t temp_time1 = 0, temp_time2 = 0;
    uint32_t temp_date1 = 0, temp_date2 = 0;

    for (i = 0; i < TIMES_TO_GET_TR; i++)
    {
        temp_time1 = std_rtc_time_get_time();
        temp_date1 = std_rtc_date_get_date();
        temp_time2 = std_rtc_time_get_time();
        temp_date2 = std_rtc_date_get_date();
       // b_log("temp_time1: %d, temp_time2: %d, temp_date1: %d, temp_date2: %d\n", temp_time1, temp_time2, temp_date1, temp_date2);
        if ((temp_time1 == temp_time2) && (temp_date1 == temp_date2))
        {
            read_ok = 1;
            break;
        }
    }

    if (read_ok)
    {
        time->hour   = std_rtc_convert_bcd2bin((temp_time1 >> RTC_OFFSET_HOUR) & 0xFF);
        time->minute = std_rtc_convert_bcd2bin((temp_time1 >> RTC_OFFSET_MINUTE) & 0xFF);
        time->second = std_rtc_convert_bcd2bin(temp_time1 & 0xFF);

        time->year  = std_rtc_convert_bcd2bin((temp_date2 >> RTC_OFFSET_YEAR) & 0xFF);
        time->month = std_rtc_convert_bcd2bin((temp_date2 >> RTC_OFFSET_MONTH) & 0xFF);
        time->day   = std_rtc_convert_bcd2bin(temp_date2 & 0xFF);
        time->weekday = (temp_date2>>24) & 0xF;
        time->weekday = (time->weekday == 7) ? 0 : time->weekday;
    }

    else
    {
        return -1;
    }

    return 0;
}

int bMcuRtcSetAlarm(bRtcTime_t *time)
{

    return 0;
}

int bMcuRtcGetAlarm(bRtcTime_t *time)
{

    return 0;
}

int bMcuRtcEnableAlarm(uint8_t enable)
{

    return 0;
}